<template>
  <div class="app-container calendar-list-container">
    <!-- filter 条件查询 -->
    <div class="filter-container">
      <el-input
            @keyup.enter.native="handleFilter"
            clearable
            v-model="listQuery.technologyNo"
            style="width: 180px;margin-right:15px;margin-top:7px"
            class="filter-item"
            placeholder="请输入产品编号"
      ></el-input>
      <el-button class="filter-item" type="primary" v-waves icon="el-icon-search" @click="handleFilter">搜索</el-button>
    </div>

    <!-- table 表格 -->
    <el-table  :data="tableInfo" v-loading="loadingList" element-loading-text="给我一首歌的时间" stripe border fit highlight-current-row style="width: 100%">
      <el-table-column  label="序号" type="index" align="center" width="80px"/>
      <el-table-column  label="产品编号" prop='technologyNo' align="center" width="150px"/>
      <el-table-column  label="工序" prop='tprocessOrder' align="center" width="150px"/>
      <el-table-column  label="PDF文件URL" align="center" width="500px">
        <template slot-scope="scope">
            <div v-for="(item,index) in scope.row.pdfUrls" :key="index">
              {{item}}
            </div>
        </template>
      </el-table-column>
      <el-table-column  label="视频文件URL" prop='videoUrl' align="center" width="500px"/>
      <!-- 对员工数据进行编辑与删除操作 -->
      <el-table-column  label="操作" align="center" v-if="userInfo.roleId != 37">
        <template slot-scope="scope" >
              <el-button size="mini" type="danger" @click="handleDelete(scope.$index,scope.row)">删除</el-button>
              <el-button type="danger" @click="Delete(scope.$index,scope.row)" icon="el-icon-delete" circle></el-button>
        </template>
      </el-table-column>
    </el-table>
   
   <!-- pagination 分页 -->
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="listQuery.page"
        :page-sizes="[5,10, 20, 30, 40, 100, 300, 500]"
        :page-size="listQuery.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>
 
  </div>
</template>

<script>
import { technologyFilePage, delTechnologyFile,deleteFiles } from '@/api/filesUpload.js'
export default {
  name:'change_Pdf',
  data() {
    return {
      total: '',                                    //数据的总数
      listQuery: {
          technologyNo: '',                         //产品编号
          page: 1,                                  //页码
          limit: 10                                 //每页显示的数据数
      },
      tableInfo: null,                              // 将接收的用户列表数据进行筛选，只取其中的record数组数据，tableInfo为一个数组
      loadingList: false,                            // 控制table的加载动画
      userInfo: this.$store.state.user.userInfo,
    }
  },
  methods: {
    // 搜索按钮绑定的函数，向服务器发送表单数据
    // 所有传输的表单字段都不是必须的，按需查询
    async handleFilter() {
      this.loadingList = true
      let query = Object.assign({},this.listQuery)
      // 用来判断查询的条件是否为空，当不为空时，则返回到首页，也就是第一页
      if(query.technologyNo == '') {
         console.log('搜索内容为空')
      } else {
        query.page = 1
      }
      let {data} = await technologyFilePage(query)
      data.records.forEach(item => {
        let urls = []
        urls.push(item.pdfUrl)
        urls.push(item.pdfUrl1)
        item.pdfUrls = urls
      });
      this.tableInfo = data.records
      this.total = data.total
      this.loadingList = false
    },
    // 删除用户按钮,使用elementui的messageBox弹框组件
    // 传入的数据是数据所在的行数以及该条目所有的信息
    async handleDelete(index,row) {
      this.$confirm('是否先删除资源后删除记录，否则上传的资源后续无法删除', '警告', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(async () => {
          let res = await delTechnologyFile({technologyNo: row.technologyNo,processOrder: row.tprocessOrder})
          this.tableInfo.splice(index, 1)
          this.$message({
            type: 'success',
            message: '删除成功'
          }) 
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },  
    Delete(index,row){
      this.$confirm(`是否确认删除该编码编码：<span style="color:red;">${row.technologyNo}</span>，资源：<span style="color:red;">${row.pdfUrl}</span>，该资源删除后，后续这个资源路径对应的文件将会永久删除`, '警告', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            // type: 'warning'
            dangerouslyUseHTMLString: true
        }).then(async () => {
          const res = await deleteFiles({image: row.pdfUrl})    
          if(res.code == 1){
            this.$message({
              type: 'success',
              message: res.message
            })
          } else{
            return  this.$message({
              type: 'error',
              message:  res.message
            })
          }
        })
    },
    // table表单下方的页码组件改变每页显示的数据条数
     handleSizeChange(val) {
      this.listQuery.limit = val
      this.handleFilter()
    },
    // table表单下方的页码组件改变页面
     handleCurrentChange(val) {
      this.listQuery.page = val
      this.handleFilter()
    }
  },
  // 进入页面获取table信息
  mounted() {
    this.handleFilter()
  }
}
</script>

<style>
</style>